import re
import os
import helpers

#~ TASK = 'py2cpp'  #can convert: py2img, cpp2img, py2cpp
#~ TASK = 'cpp2img'  #can convert:  py2cpp, py2img, cpp2img, *2img
TASK = 'cpp2img'
#~ NAME_SEARCH = ''
NAME_SEARCH = 'nonrandom'
LINENOS4IMG = False



html_entities = {
    '&quot;': '"',
    '&amp;': '&',
    '&nbsp;': ' ',
}
html_entities_more_less ={
    '&gt;': '>',
    '&lt;': '<',
}
html_entities.update( html_entities_more_less )

def html2txt(html):
    txt = html
    txt = txt.replace(' ', ' ') # replace mystical whitespace char with real space :)
    txt = txt.replace('<br />', '\n')
    
    re_short_tag = r'</?(b|i|u|p|span|div|pre|a|strong|font)\b[^<]{0,50}?>' # TODO: WARNING -- needs to find only real tags
    re_long_tag = r'</?(p|span|div|pre)\b[^<]*?>' # TODO: WARNING -- needs to find only real tags
    txt = re.sub(re_short_tag, '', txt) # flags=re.DOTALL - better not use DOTALL here -- as < lt gt > signs might occur in different lines like: "5<b ... a>8"...
    txt = re.sub(re_long_tag, '', txt)

    for entity, symbol in html_entities.items():
        txt = txt.replace(entity, symbol)

    #~ re_multiple_empty_lines = r'(\n(\s*)){3, 10}'
    re_multiple_empty_lines = r'\n{3,10}'
    txt = re.sub(re_multiple_empty_lines, '\n\n', txt, flags=re.DOTALL )
    
    return txt

def code2html_entities(txt):
    for entity, symbol in html_entities_more_less.items():
        txt = txt.replace(symbol, entity)
    return txt    


#~ xmldir = 'output/Moodle/xml/nonrandom/' 
#~ xmldir = 'output/Moodle/xml/_py2cpp/'
counter = 0
xmldir = 'output/Moodle/xml/' 
for x in os.listdir( xmldir ):
    
    srclang = TASK.split('2')[0]
    if not x.startswith(srclang):
        continue

    if NAME_SEARCH:                # if filter by name fragment is defined
        if not NAME_SEARCH in x:   # and it is not found
            continue               # skip
            
    if x.endswith('.xml') and not x.endswith('.xmlNEW.xml'):
        print(x)
    #~ if x.endswith("cpp-klausimai-Kintamieji_Salyga_Ciklas-20131112-0815.xml"):
    #~ if x.endswith("klausimai-Funkcijos-20131112-0845.xml"):
    #~ if x.endswith("klausimai-TFKTPROGRAM1-Programos sekimas-20131112-0812.xml"):
        xmlfilename =  x
        topic = xmlfilename.split('-', 2)[1]

        xml = open(xmldir + xmlfilename).read()
        re_question_typeonly_tpl = r'<question type="(.*?)">.*?</question>'
        re_name_tpl = r'<name>\s*<text>(.*?)</text>\s*</name>'
        re_questiontext_tpl = r'<questiontext format="(.*?)">\s*<text>(.*?)</text>\s*</questiontext>'
        re_question_details_tpl = r'<question type="(.*?)">.*?'+re_name_tpl+r'.*?'+re_questiontext_tpl+r'.*?</question>'
        #~ print( re_question_details_tpl )
        re_question_typeonly = re.compile(re_question_typeonly_tpl, re.DOTALL)
        re_question_details = re.compile(re_question_details_tpl, re.DOTALL)

        
        newxml = xml
        names=[]
        category = None
        for match_typeonly in re_question_typeonly.finditer(xml, re.DOTALL):
            questiontype = match_typeonly.group(1)
            question = match_typeonly.group(0)
            #~ print("|||    ", question , "   |||")
            if questiontype == 'category':
                category = question.split("<text>")[1].split("</text>")[0]
                print("\n== CATEGORY == ", category.split('/')[-1])
                continue
            if questiontype in ['calculated', 'shortanswer', 'multichoice', 'numerical']:  # calculated might be a mess                
            #~ if questiontype in ['calculated']:  # calculated might be a mess                
                match_details = re_question_details.match(question)
                questiontype, name, textformat, text = match_details.groups() 
                if text.startswith('<![CDATA['):
                    text = text[len('<![CDATA['):-len(']]>')]
                else:
                    newxml = newxml.replace(text, '<![CDATA['+text+']]>')
                intro, code, html = None, text, None
                code_original = ''
                
                print("\n\n###", helpers.escape_path(name), questiontype, textformat,
                        #~ "\n-------------\n", text,
                        #~ "\n----------------------\n", code,
                        #~ "\n----------------------\n", html
                    )
                names.append(name)                                
                if textformat == 'html':
                    if text.startswith('<p>') and text.endswith('</p>'):
                        text = text[len('<p>'):-len('</p>')]
                    #~ if text.count("</p>") > 1:
                        #~ print("WARNING", name, ": many <p> - migt cause trouble")
                    #~ if text.count("</p>") > 0:
                        #~ re_p = re.compile(r"<p ?.*?>(.*?)</p>", re.DOTALL)
                        #~ match = re_match(text)
                        #~ if match:
                            #~ text = match.group(1)
                        
                    if questiontype == 'calculated':

                        # find placeholders {a}  {b}  {c} ...
                        lines = text.split('<br />')
                        lastline_defined_value = -1 # for Python should be ok, for C++ not
                        for nr, line in enumerate(lines):
                            #~ has_defined_value = False
                            for a in 'abc':
                                if '{'+a+'}' in line:
                                    lastline_defined_value = nr
                                
                        if lastline_defined_value != -1:
                            intro = '<br />'.join(lines[:lastline_defined_value+1]) + '<br />'
                            code = '<br />'.join(lines[lastline_defined_value+1:])
                        else:
                            intro, code = '', text

                        code_original = code
                        code = html2txt(code)

                    else:
                        intro, code = '', text

                        if '<pre' in text:
                            re_prehtml=re.compile(r"(.*?)<pre.*?>(.*?)</pre>", re.DOTALL)
                            match = re_prehtml.search(text)
                            if match:
                                intro = match.group(1)
                                code = match.group(2)

                                code_original = code
                                code = html2txt(code)
                                    
                        else:
                            #~ # and strip other tags?
                            if '?' in text:                    
                                intro, code = text.split("?", 1) 
                                intro += "?"
                                if '<br />' in code:                    
                                    intro2, code = code.split('<br />', 1) 
                                    intro += intro2 + '<br />'                                
                                
                            elif '<br />' in text:                    
                                intro, code = text.split('<br />', 1) 
                                intro += '<br />'
                        
                            code_original = code
                            code = html2txt(code)
                            #~ print( "code ", code )
                            #~ print( "code_original ", code_original )
                            

                        
                if textformat == 'markdown':
                    if "?" in text:
                        intro, code = text.split("?", 1) # expects ? linebreaks before code
                        intro += "?"
                        if '\n' in code:
                            intro_end, code = code.split('\n', 1)
                            intro += intro_end+'\n'                
                    elif "\n" in text:
                        intro, code = text.split("\n", 1) # expects (two) linebreaks before code

                if code_original or code:

                    if TASK=='py2cpp':
                        
                        import py2cpp
                        print( code, "\n----------------------\n")
                        translated_code = py2cpp.convert( code )
                        print(translated_code , "\n----------------------\n")
                        if textformat == 'html':
                            #~ translated_code = code2html_entities(translated_code)
                            translated_code = "<pre>\n%s\n</pre>" % translated_code
                        newxml = newxml.replace(code_original or code, translated_code)
                        
                        
                    if '2img' in TASK:
                        lang=TASK[:-len('2img')]
                        #~ counter+=1
                        #~ name_without_hints="Errors_%"% counter, 
                        helpers.code_highlight(
                                name, 
                                lang=lang,
                                code=code,
                                LINENOS4IMG=LINENOS4IMG,
                        )
                        # insert link to image
                        if textformat in ('html', 'markdown'):
                            newxml = newxml.replace(
                                code_original or code,
                                helpers.img_placer(textformat, img_name=lang+'_'+name)
                            )


        langNames = dict(py="Python", cpp="C++", java="Java", php="PHP")

        category_trunk =  '$system$/%s/' % langNames[srclang] #like: $system$/Python/....
        if not category_trunk in newxml: 
            category_trunk = '$system$/'  # just 
        newxml = newxml.replace(category_trunk, category_trunk+TASK.upper()+'/' )
            
        if 'nonrandom' in xmldir:
            xmldir = xmldir.replace('/nonrandom', '')
            xmlfilename = 'nonrandom-' + xmlfilename
            
        if TASK.endswith('2img'):
        # convert to img
            with open(xmldir + '_code2img/' + xmlfilename+"NEW.xml", 'w') as f:
                f.write(newxml)
        else:
        # translate to other lang
            targetlang = TASK.split('2')[1]
            prefix = '_'+TASK+'/'+targetlang+'-'+TASK + '-' 
            with open(xmldir + prefix + xmlfilename, 'w') as f:
                f.write(newxml)
##############  phantomjs code
# https://github.com/ariya/phantomjs/blob/master/examples/render_multi_url.js
# https://github.com/ariya/phantomjs/blob/master/examples/scandir.js
# http://www.cambus.net/creating-thumbnails-using-phantomjs-and-imagemagick/
# http://phantomjs.org/screen-capture.html
# http://www.kobashicomputing.com/a-look-at-phantomjs

#~ current_dir = os.path.dirname(os.path.realpath(__file__))
#~ tpl = """
#~ var pageNR = require('webpage').create();
#~ pageNR.open('PATH', function() {
  #~ pageNR.render('NAME.png');
  #~ phantom.exit();
#~ });
#~ """
#~ js_code = ""
#~ for name in names:
    #~ js
